﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PokerClient.Connection.Interfaces;
using System.Threading;
using PokerClient.VisualDisp;

namespace PokerClient
{
	public partial class MainWindow : Form, IChangeWindow
	{
		
		delegate void setText(String s);
		Client myClient;
		Boolean isConnected = false;
		public MainWindow()
		{
			InitializeComponent();
			myClient = new Client(this);
			myClient.currentWindow = this;
		}


		public void updateStatus(String s)
		{
			if (this.statusStripText.InvokeRequired)
			{
				setText st = new setText(updateStatus);
				this.statusStripText.Invoke(st, new object[] { s });
			}
			else
			{
				this.statusStripText.Text = s;
				if (isConnected) 
				{ 
					switch(s)
					{
						case "AuthSuccess":
							this.changeScreen("Lobby");
							break;
						default:
							this.resetStuff();
							return;
					}

				}
			}

		}

		private Boolean validatePass()
		{
			return (this.suPass.Text == this.suVPass.Text);
		}

		public void changeScreen(string formname)
		{
			Thread chgwindow = new Thread(new ThreadStart(chgwdo));
			chgwindow.Start();
			this.Dispose();
			
		}

		private void chgwdo()
		{
		
			Application.Run(new Lobby(myClient));
		}

		internal void sUsernChips(string username, string chips)
		{
			myClient.usern = username;
			myClient.chip = chips;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			try
			{

				if (this.logUser.Text.Length > 0 && this.logPass.Text.Length > 0)
				{
					//myClient.usern = this.logUser.Text;
					//	myClient.pass = this.logPass.Text;

					if (!isConnected)
					{
						try
						{

							this.button1.Enabled = false;
							myClient.go();
							Thread.Sleep(100);
							myClient.auth(this.logUser.Text, this.logPass.Text);
							isConnected = true;

						}
						catch (Exception ex)
						{
							this.resetStuff();
							updateStatus(ex.Message);


						}

					}
				}
				else
					this.updateStatus("Blank Fields!!!!");
			}

			catch(Exception ex)
			{
				this.updateStatus(ex.Message);
			}
				
		}

		private void button2_Click(object sender, EventArgs e)
		{
			try
			{
				if (suUser.Text.Length > 0 && suPass.Text.Length > 0 && suVPass.Text.Length > 0)
				{
					if (this.validatePass())
					{
						try
						{
							myClient.go();
							Thread.Sleep(500);
							myClient.reg(this.suUser.Text, this.suVPass.Text);
							Thread.Sleep(500);
							this.myClient.reset();
						}
						catch (System.Exception ex)
						{
							this.updateStatus(ex.Message);
							this.resetStuff();
						}
						this.resetStuff();

					}
				}
				else
					this.updateStatus("Blank FIELDS!!!");
			}
			catch (Exception ex)
			{
				this.updateStatus(ex.Message);
			}
		}

 
		private void resetStuff()
		{
			this.button1.Enabled = true;
			this.button2.Enabled = true;
			this.myClient.reset();
			Thread.Sleep(100);
			this.myClient = new Client(this);
			this.isConnected = false;
		}
	

	}
	

	
}
